package com.example.jikeyoujikeyou.office_sz.view.activity.main;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.jikeyoujikeyou.office_sz.R;
import com.example.jikeyoujikeyou.office_sz.comment.utils.ListValidate;
import com.example.jikeyoujikeyou.office_sz.model.Chat;
import com.example.jikeyoujikeyou.office_sz.model.Department;
import com.example.jikeyoujikeyou.office_sz.view.activity.register.DepartmentAdapter;

import java.util.ArrayList;
import java.util.List;

public class ChatAdapter extends BaseAdapter {

    private List<Chat> chatList = new ArrayList<>();
    private Context context;
    private LayoutInflater inflater;

    public List<Chat> getChatList() {
        return chatList;
    }

    public ChatAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return ListValidate.isEmpty(chatList) ? 0 : chatList.size();
    }

    public void addItem(Chat chat){
        chatList.add(chat);
        notifyDataSetChanged();
    }

    @Override
    public Chat getItem(int position) {
        return chatList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if(null==view){
            view = inflater.inflate(R.layout.item_chat, null, false);
            viewHolder= new ViewHolder(view);
            view.setTag(viewHolder);
            viewHolder.chat_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    chatList.remove(position);
                    notifyDataSetChanged();
                }
            });
        } else {
            viewHolder= (ViewHolder) view.getTag();
        }
        viewHolder.init(getItem(position));
        return view;
    }

    class ViewHolder {

        View converView;
        View chat_delete;
        ImageView image_header;
        TextView tv_chat_name,tv_chat_seat,tv_chat_time,tv_chat_content;

        public ViewHolder(View converView) {
            this.converView = converView;
            image_header = converView.findViewById(R.id.image_header);
            tv_chat_name = converView.findViewById(R.id.tv_chat_name);
            tv_chat_seat = converView.findViewById(R.id.tv_chat_seat);
            tv_chat_time = converView.findViewById(R.id.tv_chat_time);
            tv_chat_content = converView.findViewById(R.id.tv_chat_content);
            chat_delete = converView.findViewById(R.id.chat_delete);
        }

        public void init(Chat chat) {
            image_header.setBackgroundResource(R.drawable.header_image);
            tv_chat_name.setText(chat.getName());
            tv_chat_seat.setText(chat.getSeat());
            tv_chat_time.setText(chat.getTime());
            tv_chat_content.setText(chat.getMsg());
        }

    }

}
